﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.HomeServer.SDK.Interop.v1;
using Microsoft.SharePoint;

namespace Umbrella.Whs.SharePoint
{
    public class Website : SharePointConnected, IDisposable
    {
        private readonly log4net.ILog _log = log4net.LogManager.GetLogger(typeof(Website));
        private readonly WebsiteManager _mgr;
        private readonly SPSite _site;

        public IEnumerable<WebsiteUser> GetUsers()
        {
            try
            {
                return (
                    from SPUser u in _site.RootWeb.AllUsers
                    select new WebsiteUser(u)
                    ).ToArray();
            }
            catch (Exception x)
            {
                _log.Error("Failed to retrieve all users", x);

                throw;
            }
        }

        public WebsiteUser GetUser(string loginName)
        {
            if (string.IsNullOrEmpty(loginName))
                throw new ArgumentException("null or empty", "loginName");

            try
            {
                return (
                    from SPUser u in _site.RootWeb.AllUsers
                    where u.LoginName == loginName
                    select new WebsiteUser(u)
                    ).FirstOrDefault();
            }
            catch (Exception x)
            {
                _log.Error("Failed to retrieve all users", x);

                throw;
            }
        }

        public void AddUser(IUserInfo user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            try
            {
                _site.RootWeb.AllUsers.Add(
                    user.LogOnName,
                    _mgr.CreateEmailAddress(user.LogOnName),
                    user.GetFullName(),
                    Properties.Resources.txtSharePointUserDefaultNote
                    );
            }
            catch (Exception x)
            {
                _log.Error("Failed to add user: " + user.LogOnName, x);

                throw;
            }
        }

        public void RemoveUser(IUserInfo user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            RemoveUser(user.LogOnName);
        }

        public void RemoveUser(string loginName)
        {
            if (string.IsNullOrEmpty(loginName))
                throw new ArgumentException("null or empty", "loginName");

            try
            {
                _site.RootWeb.AllUsers.Remove(loginName);
            }
            catch (Exception x)
            {
                _log.Error("Failed to remove user: " + loginName, x);

                throw;
            }
        }

        public void Dispose()
        {
            if (_site != null)
                _site.Dispose();
        }

        public Website()
        {
            try
            {
                _mgr = new WebsiteManager();
                _site = new SPSite(_mgr.UmbrellaWebsiteUrl.ToString());
            }
            catch (Exception x)
            {
                _log.Error("Failed to connect to website", x);

                throw;
            }
        }
    }
}
